package com.playtruly.wanzhende2025

import android.content.Context
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

/**
 * 高德地图Platform View工厂类
 * 用于创建AmapFlutterMapView实例
 */
class MapViewFactory(private val binaryMessenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {

    companion object {
        const val VIEW_TYPE = "amap_flutter_map"
        private const val TAG = "MapViewFactory"
    }

    override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
        android.util.Log.d(TAG, "🏭 =============== MapViewFactory 创建地图视图 ===============")
        android.util.Log.d(TAG, "📦 viewId: $viewId")
        android.util.Log.d(TAG, "📦 args: $args")
        
        if (context == null) {
            android.util.Log.e(TAG, "❌ Context为空，无法创建地图视图")
            throw IllegalArgumentException("Context cannot be null")
        }

        val creationParams = when (args) {
            is Map<*, *> -> args as? Map<String, Any>
            else -> null
        }

        android.util.Log.d(TAG, "📦 创建参数: $creationParams")
        
        val mapView = AmapFlutterMapView(context, viewId, binaryMessenger, creationParams)
        
        android.util.Log.d(TAG, "✅ 地图视图创建成功")
        android.util.Log.d(TAG, "🏁 =============== MapViewFactory 创建完成 ===============")
        
        return mapView
    }
}
